<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="任务" prop="taskName">
        <el-input
          size="small"
          v-model="queryParams.taskName"
          placeholder="请输入标注任务名称，多级目录名之间用@连接"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item v-if="checkrole(['admin'])" label="质检结果记录id" prop="mQaResultId">
        <el-input
          v-model="queryParams.mQaResultId"
          placeholder="请输入质检结果记录id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建人" prop="createBy">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入创建人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="文件名" prop="fileName">
        <el-input
          v-model="queryParams.fileName"
          placeholder="请输入文件名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="追踪id" prop="trackId">
        <el-input
          v-model="queryParams.trackId"
          placeholder="请输入追踪id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="标注类型" prop="mType">
        <el-select v-model="queryParams.mType" placeholder="请选择标注类型2d/3d" clearable size="small">
          <el-option
            v-for="dict in mTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="错误类型" prop="errorType">
        <el-select v-model="queryParams.errorType" placeholder="请选择错误类型" clearable size="small">
          <el-option
            v-for="dict in errorTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mark:remarkBall:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['mark:remarkBall:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['mark:remarkBall:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['mark:remarkBall:export']"
          >导出</el-button
        >
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="remarkBallList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="文件名" align="center" prop="fileName" />
      <el-table-column label="追踪id" align="center" prop="trackId" />
      <el-table-column label="标注类型2d/3d" align="center" prop="mType" :formatter="mTypeFormat" />
      <el-table-column label="错误类型" align="center" prop="errorType" :formatter="errorTypeFormat" />
      <el-table-column label="批注" align="center" prop="remark" />
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column v-if="checkrole(['admin'])" label="修改人" align="center" prop="updateBy" />
      <el-table-column v-if="checkrole(['admin'])" label="修改时间" align="center" prop="updateTime" />
      <el-table-column label="状态" align="center" prop="delFlag" :formatter="delFlagFormat" />
      <el-table-column v-if="checkrole(['admin'])" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['mark:remarkBall:edit']"
            >修改</el-button
          >
          <el-button
            v-if="scope.row.delFlag && scope.row.delFlag == 2"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleRestore(scope.row)"
            v-hasPermi="['mark:remarkBall:remove']"
            >恢复</el-button
          >
          <el-button
            v-if="!scope.row.delFlag || scope.row.delFlag == 0"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mark:remarkBall:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改验收球记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标注任务id" prop="taskId">
          <el-input v-model="form.taskId" placeholder="请输入标注任务id" />
        </el-form-item>
        <el-form-item label="质检结果记录id" prop="mQaResultId">
          <el-input v-model="form.mQaResultId" placeholder="请输入质检结果记录id" />
        </el-form-item>
        <el-form-item label="文件名" prop="fileName">
          <el-input v-model="form.fileName" placeholder="请输入文件名" />
        </el-form-item>
        <el-form-item label="追踪id" prop="trackId">
          <el-input v-model="form.trackId" placeholder="请输入追踪id" />
        </el-form-item>
        <el-form-item label="标注类型2d/3d">
          <el-select v-model="form.mType" placeholder="请选择标注类型2d/3d">
            <el-option
              v-for="dict in mTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="错误类型">
          <el-select v-model="form.errorType" placeholder="请选择错误类型">
            <el-option
              v-for="dict in errorTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="x" prop="x">
          <el-input v-model="form.x" placeholder="请输入x" />
        </el-form-item>
        <el-form-item label="y" prop="y">
          <el-input v-model="form.y" placeholder="请输入y" />
        </el-form-item>
        <el-form-item label="z" prop="z">
          <el-input v-model="form.z" placeholder="请输入z" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listRemarkBallWithDataScope,
  listRemarkBallWithoutDataScope,
  getRemarkBall,
  delRemarkBall,
  addRemarkBall,
  updateRemarkBall,
  exportRemarkBall,
} from '@/api/mark/remarkBall'
import { checkRole } from '@/utils/permission'

export default {
  name: 'RemarkBall',
  data() {
    return {
      // 验收任务树选项
      qaTaskOptions: [],
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 验收球记录表格数据
      remarkBallList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 标注类型2d/3d字典
      mTypeOptions: [],
      // 错误类型字典
      errorTypeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        taskId: undefined,
        mQaResultId: undefined,
        fileName: undefined,
        trackId: undefined,
        mType: undefined,
        errorType: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        taskId: [{ required: true, message: '标注任务id不能为空', trigger: 'blur' }],
        mQaResultId: [{ required: true, message: '质检结果记录id不能为空', trigger: 'blur' }],
        fileName: [{ required: true, message: '文件名不能为空', trigger: 'blur' }],
      },
    }
  },
  created() {
    this.getDicts('mark_result_type').then((response) => {
      this.mTypeOptions = response.data
    })
    this.getDicts('mark_result_error_type').then((response) => {
      this.errorTypeOptions = response.data
    })
  },
  methods: {
    checkrole(role) {
      return checkRole(role)
    },
    /** 转换验收任务数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children
      }
      return {
        id: node.taskId,
        label: node.taskName,
        children: node.children,
      }
    },
    /** 查询验收球记录列表 */
    getList() {
      this.loading = true
      if (this.checkrole(['qa'])) {
        listRemarkBallWithDataScope(this.queryParams).then((response) => {
          this.remarkBallList = response.rows
          this.total = response.total
          this.loading = false
        })
      } else {
        if (this.queryParams.taskName) {
          listRemarkBallWithoutDataScope(this.queryParams).then((response) => {
            this.remarkBallList = response.rows
            this.total = response.total
            this.loading = false
          })
        } else {
          alert('请输入任务名称')
          this.loading = false
        }
      }
    },
    // 标注类型2d/3d字典翻译
    mTypeFormat(row, column) {
      return this.selectDictLabel(this.mTypeOptions, row.mType)
    },
    // 错误类型字典翻译
    errorTypeFormat(row, column) {
      return this.selectDictLabel(this.errorTypeOptions, row.errorType)
    },
    // 错误类型字典翻译
    delFlagFormat(row, column) {
      return row.delFlag == '0' ? '有效' : '已删除'
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        remarkId: undefined,
        taskId: undefined,
        mQaResultId: undefined,
        fileName: undefined,
        trackId: undefined,
        mType: undefined,
        errorType: undefined,
        remark: undefined,
        x: undefined,
        y: undefined,
        z: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.remarkId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加验收球记录'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const remarkId = row.remarkId || this.ids
      getRemarkBall(remarkId).then((response) => {
        this.form = response.data
        this.open = true
        this.title = '修改验收球记录'
      })
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.remarkId != undefined) {
            updateRemarkBall(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess('修改成功')
                this.open = false
                this.getList()
              } else {
                this.msgError(response.msg)
              }
            })
          } else {
            addRemarkBall(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess('新增成功')
                this.open = false
                this.getList()
              } else {
                this.msgError(response.msg)
              }
            })
          }
        }
      })
    },
    /** 恢复按钮操作 */
    handleRestore(row) {
      let params = {
        remarkId: row.remarkId,
        delFlag: 0,
      }
      updateRemarkBall(params).then((response) => {
        if (response.code === 200) {
          this.msgSuccess('批注恢复成功')
          this.getList()
        } else {
          this.msgError(response.msg)
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const remarkIds = row.remarkId || this.ids
      this.$confirm('是否确认删除验收球记录编号为"' + remarkIds + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(function () {
          return delRemarkBall(remarkIds)
        })
        .then(() => {
          this.getList()
          this.msgSuccess('删除成功')
        })
        .catch(function () {})
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$confirm('是否确认导出所有验收球记录数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(function () {
          return exportRemarkBall(queryParams)
        })
        .then((response) => {
          this.download(response.msg)
        })
        .catch(function () {})
    },
  },
}
</script>
